// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        background:cc.Node,
        selectAreaBtns:[cc.Button],
        answerAreaBtns:[cc.Button],
        tipLayer:cc.Node,
        tipLabel:cc.Label,
      },
  
      onLoad () {},
  
      start () {
          //设置背景图高度和屏幕高度一样
          this.background.height = cc.view.getVisibleSize().height;
          var i = cc.sys.localStorage.getItem("TIPTIMES");
          this.tipLabel.string = ""+i;

          this.datasource = [{answer:"一意孤行",hxitem:"狐假虎威",tip:"",selectItem:[],image:""}];

          
          this.initData();
      },
      initData:function(){
        var i = cc.sys.localStorage.getItem("START_LEVEL");
        if(i==null) {
        cc.sys.localStorage.setItem("START_LEVEL",'0');
        i = 0;
        }else  i = parseInt(i);

        this.answers = [{'id':"-1","word":""},{'id':"-1","word":""},{'id':"-1","word":""},{'id':"-1","word":""}];

        var obj = this.datasource[i];
        var j = Math.floor(Math.random()*10000)%4 +1;
        var word = obj.answer[j-1];
        var tip = "第"+j+"个字是"+" —— "+word;
        obj.tip = tip;

        var s =  "行尸走肉金蝉脱壳百里挑一金玉满堂背水一战霸王别姬天上人间不吐不快海阔天空情非得已满腹经纶兵临城下春暖花开插翅难逃黄道吉日天下无双偷天换日两小无猜卧虎藏龙珠光宝气簪缨世族花花公子绘声绘影国色天香相亲相爱八仙过海金玉良缘掌上明珠皆大欢喜逍遥法外生财有道极乐世界情不自禁愚公移山魑魅魍魉龙生九子精卫填海海市蜃楼高山流水擎天柱卧薪尝胆壮志凌云金枝玉叶四海一家穿针引线无忧无虑无地自容三位一体落叶归根相见恨晚惊天动地滔滔不绝相濡以沫长生不死原来如此女娲补天三皇五帝万箭穿心水木清华窈窕淑女过五关斩六将温文尔雅暗度陈仓甘心情愿挑肥拣瘦阿猫阿狗心有余悸数典忘祖喜出望外文过饰非连锁反应将心比心无动于衷鹤唳华亭妙手空空登峰造极惊涛骇浪自欺欺人绿树成荫岂有此理败家子万马齐喑世态炎凉冠冕堂皇天罗地网踽踽独行兔死狐悲众志成城耳提面命周瑜打黄盖东山再起丹凤朝阳和光同尘心力衰竭事半功倍阿鼻地狱九关虎豹劝百讽一琳琅满目一丝不苟逝者如斯同仇敌忾朝秦暮楚夏虫不可以语冰不亦乐乎苛政猛于虎哭笑不得重见天日集腋成裘风月无边乐此不疲咫尺天涯宠辱不惊安然无恙一事无成若即若离本末倒置秋风落叶无价之宝富贵不能淫";
        var array = [];
        for(var i=0;i<s.length;i++){array.push(s[i]);}
        function randomsort(a, b) { return Math.random()>.5 ? -1 : 1;}
        array.sort(randomsort);

        this.array = [];
        var s = obj.answer+obj.hxitem;
        for(var i =0;i<s.length;i++){  this.array.push(s[i]);}   
        for(var i =0;i<16;i++){ this.array.push(array[i]);}
        this.array.sort(randomsort);
        obj.selectItem = this.array;

        for(var i=0;i<24;i++)
        {
            var word = this.array[i];
            var btn = this.selectAreaBtns[i];
            var childNode = btn.node.getChildByName("Label");
            childNode.getComponent(cc.Label).string = word;
        }

        for(var i=0;i<4;i++)
        {
          var item = this.answers[i];
          var btn = this.answerAreaBtns[i];
          var childNode = btn.node.getChildByName("Label");
          childNode.getComponent(cc.Label).string = item.word;
        }
        
      },

      setAnswerArea:function(){

      },

      backBtnClicked:function(){
        cc.director.loadScene('main');
      },

      /* 答案区域按钮点击事件 */
      answerBtnClicked:function(event,id){
        console.log("id = ",id);
        if(-1==id) return;

        var item = this.answers[id];

        {
          var btn = this.selectAreaBtns[item.id];
          btn.node.active = true;
        } 
      
        item.id = "-1";
        item.word = "";
        var btn = this.answerAreaBtns[id];
        var childNode = btn.node.getChildByName("Label");
        childNode.getComponent(cc.Label).string = "";
      },

      /*选取按钮点击事件 */
      selectBtnClicked:function(event,id){
        console.log("id = ",id);
        for (var i=0;i<4;i++){
          var item = this.answers[i];
          if(''==item.word)
          {
            item.id = id;
            item.word = this.array[id];
            var btn = this.selectAreaBtns[id];
            btn.node.active = false;
            var btn = this.answerAreaBtns[i];
            var childNode = btn.node.getChildByName("Label");
            childNode.getComponent(cc.Label).string = item.word;
            this.checkResult();
            return;
          }
        }
      },

      checkResult(){
        var flag = true;
        for (var i=0;i<4;i++){
          var item = this.answers[i];
          if(''==item.word){
            flag = false;
            break;
          }  
        }
        if(flag == false) return;
        var s = this.answers[0].word+this.answers[1].word+this.answers[2].word+this.answers[3].word;
        var i = cc.sys.localStorage.getItem("START_LEVEL");
        var tm = this.datasource[parseInt(i)];
        if(s == tm.answer)
        {
          console.log("user answer right");
        }
        else
        {
          console.log("user anser error");
        }
      },
      tipBtnClicked(){
        var i = cc.sys.localStorage.getItem("TIPTIMES");
        i = parseInt(i);
        if(--i>=0){
          this.tipLabel.string = ""+i;
          cc.sys.localStorage.setItem("TIPTIMES",''+i);
          this.tipLayer.active = true;
          var i = parseInt(cc.sys.localStorage.getItem("START_LEVEL"));
          var obj = this.datasource[i];
          var j = Math.floor(Math.random()*10000)%4 +1;
          var word = obj.answer[j-1];
          var tip = "第"+j+"个字是"+" —— "+word;
          var childNode = this.tipLayer.getChildByName("Label");
          childNode.getComponent(cc.Label).string = tip;
        }
      },
      shareBtnClicked(){
        console.log('shareBtnClicked');
      },
      tipClose(){
        this.tipLayer.active = false;
      }
    // update (dt) {},
});
